home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Utilities / vim-5.1 / doc / autocmd.txt next >
Encoding:
Text File  |  1998-04-05  |  23.6 KB  |  573 lines

  1. *autocmd.txt*   For Vim version 5.1.  Last modification: 1998 Apr 03
  2.  
  3.  
  4.           VIM REFERENCE MANUAL    by Bram Moolenaar
  5.  
  6.  
  7. Automatic commands                    *autocommand*
  8.  
  9. 1. Introduction            |autocmd-intro|
  10. 2. Defining autocommands    |autocmd-define|
  11. 3. Removing autocommands    |autocmd-remove|
  12. 4. Listing autocommands        |autocmd-list|
  13. 5. Events            |autocmd-events|
  14. 6. Patterns            |autocmd-patterns|
  15. 7. Groups            |autocmd-groups|
  16. 8. Executing autocommands    |autocmd-execute|
  17. 9. Using autocommands        |autocmd-use|
  18.  
  19. {Vi does not have any of these commands}
  20.  
  21. ==============================================================================
  22. 1. Introduction                        *autocmd-intro*
  23.  
  24. You can specify commands to be executed automatically for when reading or
  25. writing a file, when entering or leaving a buffer or window, and when exiting
  26. Vim.  For example, you can create an autocommand to set the 'cindent' option
  27. for files matching *.c.  You can also use autocommands to implement advanced
  28. features, such as editing compressed files (see |gzip-example|).  The usual
  29. place to put autocommands is in your .vimrc or .exrc file.
  30.  
  31. WARNING: Using autocommands is very powerful, and may lead to unexpected side
  32. effects.  Be careful not to destroy your text.
  33. - It's a good idea to do some testing on an expendable copy of a file first.
  34.   For example: If you use autocommands to decompress a file when starting to
  35.   edit it, make sure that the autocommands for compressing when writing work
  36.   correctly.
  37. - Be prepared for an error halfway through (e.g., disk full).  Vim will mostly
  38.   be able to undo the changes to the buffer, but you may have to clean up the
  39.   changes to other files by hand (e.g., compress a file that has been
  40.   decompressed).
  41. - If the BufRead* events allow you to edit a compressed file, the FileRead*
  42.   events should do the same (this makes recovery possible in some rare cases).
  43.   It's a good idea to use the same autocommands for the File* and Buf* events
  44.   when possible.
  45.  
  46. The |+autocmd| feature is only included if it has not been disabled at compile
  47. time.
  48.  
  49. ==============================================================================
  50. 2. Defining autocommands                *autocmd-define*
  51.  
  52. Note: The ":autocmd" command cannot be followed by another command, since any
  53. '|' is considered part of the command.
  54.  
  55.                             *:au* *:autocmd*
  56. :au[tocmd] [group] {event} {pat} [nested] {cmd}
  57.             Add {cmd} to the list of commands that Vim will
  58.             execute automatically on {event} for a file matching
  59.             {pat}.  Vim always adds the {cmd} after existing
  60.             autocommands, so that the autocommands execute in the
  61.             order in which they were given.  See |autocmd-nest|
  62.             for [nested].
  63.  
  64. Note that special characters (e.g., "%", "<cword>") in the ":autocmd"
  65. arguments are not expanded when the autocommand is defined.  These will be
  66. expanded when the Event is recognized, and the {cmd} is executed.  The only
  67. exception is that "<sfile>" is expanded when the autocmd is defined.  Example:
  68.  
  69. >    :au BufEnter *.html so <sfile>:h/html.vim
  70.  
  71. Here Vim expands <sfile> to the name of the file containing this line.
  72.  
  73. When your .vimrc file is sourced twice, the autocommands will appear twice.
  74. To avoid this, put this command in your .vimrc file, before defining
  75. autocommands:
  76.  
  77. >    :autocmd!    " Remove ALL autocommands.
  78.  
  79. If you don't want to remove all autocommands, you can instead use a variable
  80. to ensure that Vim includes the autocommands only once:
  81.  
  82. >    :if !exists("autocommands_loaded")
  83. >    :  let autocommands_loaded = 1
  84. >    :  au ...
  85. >    :endif
  86.  
  87. When the [group] argument is not given, Vim uses the current group (as defined
  88. with ":augroup"); otherwise, Vim uses the group defined with [group].  Note
  89. that [group] must have been defined before.  You cannot define a new group
  90. with ":au group ..."; use ":augroup" for that.
  91.  
  92. While testing autocommands, you might find the 'verbose' option to be useful:
  93. >    :set verbose=9
  94. This setting makes Vim echo the autocommands as it executes them.
  95.  
  96. ==============================================================================
  97. 3. Removing autocommands                *autocmd-remove*
  98.  
  99. :au[tocmd]! [group] {event} {pat} [nested] {cmd}
  100.             Remove all autocommands associated with {event} and
  101.             {pat}, and add the command {cmd}.  See |autocmd-nest|
  102.             for [nested].
  103.  
  104. :au[tocmd]! [group] {event} {pat}
  105.             Remove all autocommands associated with {event} and
  106.             {pat}.
  107.  
  108. :au[tocmd]! [group] * {pat}
  109.             Remove all autocommands associated with {pat} for all
  110.             events.
  111.  
  112. :au[tocmd]! [group] {event}
  113.             Remove ALL autocommands for {event}.
  114.  
  115. :au[tocmd]! [group]    Remove ALL autocommands.
  116.  
  117. When the [group] argument is not given, Vim uses the current group (as defined
  118. with ":augroup"); otherwise, Vim uses the group defined with [group].
  119.  
  120. ==============================================================================
  121. 4. Listing autocommands                    *autocmd-list*
  122.  
  123. :au[tocmd] [group] {event} {pat}
  124.             Show the autocommands associated with {event} and
  125.             {pat}.
  126.  
  127. :au[tocmd] [group] * {pat}
  128.             Show the autocommands associated with {pat} for all
  129.             events.
  130.  
  131. :au[tocmd] [group] {event}
  132.             Show all autocommands for {event}.
  133.  
  134. :au[tocmd] [group]    Show all autocommands.
  135.  
  136. If you provide the [group] argument, Vim lists only the autocommands for
  137. [group]; otherwise, Vim lists the autocommands for ALL groups.  Note that this
  138. argument behavior differs from that for defining and removing autocommands.
  139.  
  140. ==============================================================================
  141. 5. Events                        *autocmd-events*
  142.  
  143.                     *autocommand-events* *{event}*
  144. Vim recognizes the following events.  Vim ignores the case of event names
  145. (e.g., you can use "BUFread" or "bufread" instead of "BufRead").
  146.  
  147.                             *BufNewFile*
  148. BufNewFile            When starting to edit a file that doesn't
  149.                 exist.  Can be used to read in a skeleton
  150.                 file.
  151.                             *BufReadPre*
  152. BufReadPre            When starting to edit a new buffer, before
  153.                 reading the file into the buffer.  Not used
  154.                 if the file doesn't exist.
  155.                         *BufRead* *BufReadPost*
  156. BufRead or BufReadPost        When starting to edit a new buffer, after
  157.                 reading the file into the buffer, before
  158.                 executing the modelines.  This does NOT work
  159.                 for ":r file".  Not used when the file doesn't
  160.                 exist.  Also used after succesfully recovering
  161.                 a file.
  162.                             *FileReadPre*
  163. FileReadPre            Before reading a file with a ":read" command.
  164.                             *FileReadPost*
  165. FileReadPost            After reading a file with a ":read" command.
  166.                 Note that Vim sets the '[ and '] marks to the
  167.                 first and last line of the read.  This can be
  168.                 used to operate on the lines just read.
  169.                             *FilterReadPre*
  170. FilterReadPre            Before reading a file from a filter command.
  171.                 Vim checks the pattern against the the name of
  172.                 the current buffer, not the name of the
  173.                 temporary file that is the output of the
  174.                 filter command.
  175.                             *FilterReadPost*
  176. FilterReadPost            After reading a file from a filter command.
  177.                 Vim checks the pattern against the the name of
  178.                 the current buffer as with FilterReadPre.
  179.                             *StdinReadPre*
  180. StdinReadPre            Before reading from stdin into the buffer.
  181.                 Only used when the "-" argument was used when
  182.                 Vim was started |--|.
  183.                             *StdinReadPost*
  184. StdinReadPost            After reading from the stdin into the buffer,
  185.                 before executing the modelines.  Only used
  186.                 when the "-" argument was used when Vim was
  187.                 started |--|.
  188.                         *BufWrite* *BufWritePre*
  189. BufWrite or BufWritePre        Before writing the whole buffer to a file.
  190.                             *BufWritePost*
  191. BufWritePost            After writing the whole buffer to a file
  192.                 (should undo the commands for BufWritePre).
  193.                             *FileWritePre*
  194. FileWritePre            Before writing to a file, when not writing the
  195.                 whole buffer.
  196.                             *FileWritePost*
  197. FileWritePost            After writing to a file, when not writing the
  198.                 whole buffer.
  199.                             *FileAppendPre*
  200. FileAppendPre            Before appending to a file.
  201.                             *FileAppendPost*
  202. FileAppendPost            After appending to a file.
  203.                             *FilterWritePre*
  204. FilterWritePre            Before writing a file for a filter command.
  205.                 Vim checks the pattern against the the name of
  206.                 the current buffer, not the name of the
  207.                 temporary file that is the output of the
  208.                 filter command.
  209.                             *FilterWritePost*
  210. FilterWritePost            After writing a file for a filter command.
  211.                 Vim checks the pattern against the the name of
  212.                 the current buffer as with FilterWritePre.
  213.                             *FileChangedShell*
  214. FileChangedShell        After Vim runs a shell command and notices
  215.                 that the modification time of the current file
  216.                 has changed since editing started.  Run in
  217.                 place of the 'has been changed' message.  See
  218.                 |timestamp|.  Useful for reloading related
  219.                 buffers which are affected by a single
  220.                 command.
  221.                             *BufEnter*
  222. BufEnter            After entering a buffer.  Useful for setting
  223.                 options for a file type.  Also executed when
  224.                 starting to edit a buffer, after the
  225.                 BufReadPost autocommands.
  226.                             *BufLeave*
  227. BufLeave            Before leaving to another buffer.  Also when
  228.                 leaving or closing the current window and the
  229.                 new current window is not for the same buffer.
  230. BufUnload                        *BufUnload*
  231.                 Before unloading a buffer.  This is when the
  232.                 text in the buffer is going to be freed.  This
  233.                 may be after a BufWritePost and before a
  234.                 BufDelete.
  235. BufDelete                        *BufDelete*
  236.                 Before deleting a buffer from the buffer list.
  237.                 The BufUnload may be called first (if the
  238.                 buffer was loaded).
  239.                             *WinEnter*
  240. WinEnter            After entering another window.  Not done for
  241.                 the first window, when Vim has just started.
  242.                 Useful for setting the window height.
  243.                 If the window is for another buffer, Vim
  244.                 executes the BufEnter autocommands after the
  245.                 WinEnter autocommands.
  246.                             *TermChanged*
  247. TermChanged            After the value of 'term' has changed.  Useful
  248.                 for re-loading the syntax file to update the
  249.                 colors, fonts and other terminal-dependent
  250.                 settings.  Executed for all loaded buffers.
  251.                             *WinLeave*
  252. WinLeave            Before leaving a window.  If the window to be
  253.                 entered next is for a different buffer, Vim
  254.                 executes the BufLeave autocommands before the
  255.                 WinLeave autocommands.
  256.                             *VimEnter*
  257. VimEnter            After doing all the startup stuff, including
  258.                 loading .vimrc files, executing the "-c cmd"
  259.                 arguments, creating all windows and loading
  260.                 the buffers in them.
  261.                             *VimLeave*
  262. VimLeave            Before exiting Vim, just after writing the
  263.                 .viminfo file.
  264.                             *User*
  265. User                Never executed automatically.  To be used for
  266.                 autocommands that are only executed with
  267.                 ":doautocmd".  See |User-function| for how to
  268.                 use this event to create functions.
  269.  
  270. For READING FILES there are three possible pairs of events.  Vim uses only one
  271. pair at a time:
  272. BufNewFile            starting to edit a non-existent file
  273. BufReadPre    BufReadPost    starting to edit an existing file
  274. FilterReadPre    FilterReadPost    read the temp file with filter output
  275. FileReadPre    FileReadPost    any other file read
  276.  
  277. Note that the autocommands for the *ReadPre events and all the Filter events
  278. are not allowed to change the current buffer (you will get an error message if
  279. this happens).  This is to prevent the file to be read into the wrong buffer.
  280.  
  281. Note that the 'modified' flag is reset AFTER executing the BufReadPost
  282. and BufNewFile autocommands.  But when the 'modified' option was set by the
  283. autocommands, this doesn't happen.
  284.  
  285. You can use the 'eventignore' option to ignore a number of events or all
  286. events.
  287.  
  288. ==============================================================================
  289. 6. Patterns                        *autocmd-patterns*
  290.  
  291. The file pattern {pat} is tested for a match against the file name in one of
  292. two ways:
  293. 1. When there is no '/' in the pattern, Vim checks for a match against only
  294.    the tail part of the file name (without its leading directory path).
  295. 2. When there is a '/' in the pattern,  Vim checks for a match against the
  296.    both short file name (as you typed it) and the full file name (after
  297.    expanding it to a full path and resolving symbolic links).
  298.  
  299. Examples:
  300. >    :autocmd BufRead *.txt        set et
  301. Set the 'et' option for all text files.
  302.  
  303. >    :autocmd BufRead /vim/src/*.c    set cindent
  304. Set the 'cindent' option for C files in the /vim/src directory.
  305.  
  306. >    :autocmd BufRead /tmp/*.c    set ts=5
  307. If you have a link from "/tmp/test.c" to "/home/nobody/vim/src/test.c", and
  308. you start editing "/tmp/test.c", this autocommand will match.
  309.  
  310. Note:  To match part of a path, but not from the root directory, use a '*' as
  311. the first character.  Example:
  312. >    :autocmd BufRead */doc/*.txt    set tw=78
  313. This autocommand will for example be executed for "/tmp/doc/xx.txt" and
  314. "/usr/home/piet/doc/yy.txt".  The number of directories does not matter here.
  315.  
  316.  
  317. Note that for all systems the '/' character is used for path separator (even
  318. MS-DOS and OS/2).  This was done because the backslash is difficult to use
  319. in a pattern and to make the autocommands portable accross different systems.
  320.  
  321. Note that using ~ in a file name (for home directory) doesn't work.  Use a
  322. pattern that matches the full path name, for example "*home/piet/.cshrc".
  323.  
  324. ==============================================================================
  325. 7. Groups                        *autocmd-groups*
  326.  
  327. Autocommands can be put together in a group.  This is useful for removing or
  328. executing a group of autocommands.  For example, all the autocommands for
  329. syntax highlighting are put in the "highlight" group, to be able to execute
  330. ":doautoall highlight BufRead" when the GUI starts.
  331.  
  332. When no specific group is selected, Vim uses the default group.  The default
  333. group does not have a name.  You cannot execute the autocommands from the
  334. default group separately; you can execute them only by executing autocommands
  335. for all groups.
  336.  
  337. Normally, when executing autocommands automatically, Vim uses the autocommands
  338. for all groups.  The group only matters when executing autocommands with
  339. ":doautocmd" or ":doautoall", or when defining or deleting autocommands.
  340.  
  341. The group name can contain any characters except white space.  The group name
  342. "end" is reserved (also in uppercase).
  343.  
  344.                             *:aug* *:augroup*
  345. :aug[roup] {name}        Define the autocmd group name for the
  346.                 following ":autocmd" commands.  The name "end"
  347.                 or "END" selects the default group.
  348.  
  349. To enter autocommands for a specific group, use this method:
  350. 1. Select the group with ":augroup {name}".
  351. 2. Delete any old autocommands with ":au!".
  352. 3. Define the autocommands.
  353. 4. Go back to the default group with "augroup END".
  354.  
  355. Example:
  356. >    :augroup uncompress
  357. >    :  au!
  358. >    :  au BufEnter *.gz    %!gunzip
  359. >    :augroup END
  360.  
  361. This prevents having the autocommands defined twice (e.g., after sourcing the
  362. .vimrc file again).
  363.  
  364. ==============================================================================
  365. 8. Executing autocommands                *autocmd-execute*
  366.  
  367. Vim can also execute Autocommands non-automatically.  This is useful if you
  368. have changed autocommands, or when Vim has executed the wrong autocommands
  369. (e.g., the file pattern match was wrong).
  370.  
  371. Note that the 'eventignore' option applies here too.  Events listed in this
  372. option will not cause any commands to be executed.
  373.  
  374.                             *:do* *:doautocmd*
  375. :do[autocmd] [group] {event} [fname]
  376.             Apply the autocommands matching [fname] (default:
  377.             current file name) for {event} to the current buffer.
  378.             You can use this when the current file name does not
  379.             match the right pattern, after changing settings, or
  380.             to execute autocommands for a certain event.
  381.             It's possible to use this inside an autocommand too,
  382.             so you can base the autocommands for one extension on
  383.             another extension.  Example:
  384. >                :au Bufenter *.cpp so ~/.vimrc_cpp
  385. >                :au Bufenter *.cpp doau BufEnter x.c
  386.             Be careful to avoid endless loops.  See |autocmd-nest|.
  387.  
  388.             When the [group] argument is not given, Vim executes
  389.             the autocommands for all groups.  When the [group]
  390.             argument is included, Vim executes only the matching
  391.             autocommands for that group.  Note: if you use an
  392.             undefined group name, Vim gives you an error message.
  393.  
  394.                         *:doautoa* *:doautoall*
  395. :doautoa[ll] [group] {event} [fname]
  396.             Like ":doautocmd", but apply the autocommands to each
  397.             loaded buffer.  Careful: Don't use this for
  398.             autocommands that delete a buffer, change to another
  399.             buffer or change the contents of a buffer; the result
  400.             is unpredictable.  this command is intended for
  401.             autocommands that set options, change highlighting,
  402.             and things like that.
  403.  
  404. ==============================================================================
  405. 9. Using autocommands                    *autocmd-use*
  406.  
  407. Before the *ReadPre event the '[ mark is set to the line just above where the
  408. new lines will be inserted.  Before the *ReadPost event the '[ mark is set to
  409. the first line that was just read, the '] mark to the last line.  Careful: '[
  410. and '] change when using commands that change the buffer.
  411.  
  412. In commands which expect a file name, you can use "<afile>" for the file name
  413. that is being read |:<afile>| (you can also use "%" for the current file
  414. name).
  415.  
  416. Examples for reading compressed files:
  417. > :autocmd! BufReadPre,FileReadPre   *.gz set bin
  418. > :autocmd! BufReadPost,FileReadPost *.gz '[,']!gunzip
  419. > :autocmd  BufReadPost,FileReadPost *.gz set nobin
  420. > :autocmd  BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " . expand("%:r")
  421.  
  422. NOTE: These examples remove any existing autocommands for the same
  423. event/pattern combination because of the '!'.
  424.  
  425. For WRITING FILES there are four possible pairs of events.  Vim uses only one
  426. pair at a time:
  427. BufWritePre    BufWritePost    writing the whole buffer
  428. FilterWritePre    FilterWritePost    writing to the temp file with filter input
  429. FileAppendPre    FileAppendPost    appending to a file
  430. FileWritePre    FileWritePost    any other file write
  431.  
  432. Note that the *WritePost commands should undo any changes to the buffer that
  433. were caused by the *WritePre commands; otherwise, writing the file will have
  434. the side effect of changing the buffer.
  435.  
  436. Before executing the autocommands, the buffer from which the lines are to be
  437. written temporarily becomes the current buffer.  Unless the autocommands
  438. change the current buffer or delete the previously current buffer, the
  439. previously current buffer is made the current buffer again.
  440.  
  441. The *WritePre and *AppendPre autocommands must not delete the buffer from
  442. which the lines are to be written.
  443.  
  444. Before executing the *WritePre and *AppendPre autocommands the '[ mark is set
  445. to the first line that will be written, the '] mark to the last line.
  446. Careful: '[ and '] change when using commands that change the buffer.
  447.  
  448. In commands which expect a file name, you can use "<afile>" for the file name
  449. that is being written |:<afile>| (you can also use "%" for the current file
  450. name).
  451.  
  452.                             *gzip-example*
  453. Examples for writing compressed files:
  454. > :autocmd! BufWritePost,FileWritePost    *.gz !mv <afile> <afile>:r
  455. > :autocmd  BufWritePost,FileWritePost    *.gz !gzip <afile>:r
  456. >
  457. > :autocmd! FileAppendPre        *.gz !gunzip <afile>
  458. > :autocmd  FileAppendPre        *.gz !mv <afile>:r <afile>
  459. > :autocmd! FileAppendPost        *.gz !mv <afile> <afile>:r
  460. > :autocmd  FileAppendPost        *.gz !gzip <afile>:r
  461.  
  462. ("<afile>:r" is the file name without the extension, see |:_%:|)
  463.  
  464. The commands executed for the BufNewFile, BufRead/BufReadPost, BufWritePost,
  465. FileAppendPost and VimLeave events do not set or reset the changed flag of the
  466. buffer.  When you decompress the buffer with the BufReadPost autocommands, you
  467. can still exit with ":q".  When you use ":undo" in BufWritePost to undo the
  468. changes made by BufWritePre commands, you can still do ":q" (this also makes
  469. "ZZ" work).  If you do want the buffer to be marked as modified, set the
  470. 'modified' option.
  471.  
  472. To execute Normal mode commands from an autocommand, use the ":normal"
  473. command.  Use with care!  If the Normal mode command is not finished, the user
  474. needs to type characters (e.g., after ":normal m" you need to type a mark
  475. name).
  476.  
  477. If you want the buffer to be unmodified after changing it, reset the
  478. 'modified' option.  This makes it possible to exit the buffer with ":q"
  479. instead of ":q!".
  480.  
  481.                             *autocmd-nest*
  482. By default, autocommands do not nest.  If you use ":e" or ":w" in an
  483. autocommand, Vim does not execute the BufRead and BufWrite autocommands for
  484. those commands.  If you do want this, use the "nested" flag for those commands
  485. in which you want nesting.  For example:
  486. >    :autocmd FileChangedShell *.c nested e!
  487. The nesting is limited to 10 levels to get out of recursive loops.
  488.  
  489. It's possible to use the ":au" command in an autocommand.  This can be a
  490. self-modifying command!  This can be useful for an autocommand that should
  491. execute only once.
  492.  
  493. There is currently no way to disable the autocommands.  If you want to write a
  494. file without executing the autocommands for that type of file, write it under
  495. another name and rename it with a shell command.
  496.  
  497. Note: When reading a file (with ":read file" or with a filter command) and the
  498. last line in the file does not have an <EOL>, Vim remembers this.  At the next
  499. write (with ":write file" or with a filter command), if the same line is
  500. written again as the last line in a file AND 'binary' is set, Vim does not
  501. supply an <EOL>.  This makes a filter command on the just read lines write the
  502. same file as was read, and makes a write command on just filtered lines write
  503. the same file as was read from the filter.  For example, another way to write
  504. a compressed file:
  505.  
  506. > :autocmd FileWritePre *.gz   set bin|'[,']!gzip
  507. > :autocmd FileWritePost *.gz  undo|set nobin
  508.  
  509.                             *autocommand-pattern*
  510. You can specify multiple patterns, separated by commas.  Here are some
  511. examples:
  512.  
  513. > :autocmd BufRead   *        set tw=79 nocin ic infercase fo=2croq
  514. > :autocmd BufRead   .letter    set tw=72 fo=2tcrq
  515. > :autocmd BufEnter  .letter    set dict=/usr/lib/dict/words
  516. > :autocmd BufLeave  .letter    set dict=
  517. > :autocmd BufRead,BufNewFile   *.c,*.h    set tw=0 cin noic
  518. > :autocmd BufEnter  *.c,*.h    abbr FOR for(i = 0; i < 3; i++)^M{^M}^[O
  519. > :autocmd BufLeave  *.c,*.h    unabbr FOR
  520.  
  521. For makefiles (makefile, Makefile, imakefile, makefile.unix, etc.):
  522.  
  523. > :autocmd BufEnter  ?akefile*    set include=^s\=include
  524. > :autocmd BufLeave  ?akefile*    set include&
  525.  
  526. To always start editing C files at the first function:
  527.  
  528. > :autocmd BufRead   *.c,*.h    1;/^{
  529.  
  530. Without the "1;" above, the search would start from wherever the file was
  531. entered, rather than from the start of the file.
  532.  
  533. To read a skeleton file for new C files:
  534.  
  535. > :autocmd BufNewFile  *.c    0r ~/.skeleton.c
  536. > :autocmd BufNewFile  *.h    0r ~/.skeleton.h
  537.  
  538. To insert the current date and time in a *.html file when writing it:
  539.  
  540. > :autocmd BufWritePre,FileWritePre *.html ks|1,20g/Last modification: /normal f:lD:read !date^MkJ's
  541.  
  542. (to insert the ^M type CTRL-V CTRL-M)
  543. You need to have a line "Last modification: <date time>" in the first 20 lines
  544. of the file for this to work.  Vim replaces <date time> (and anything in the
  545. same line after it) with the current date and time.  Explanation:
  546.     ks        mark current position with mark 's'
  547.     1,20g/pattern/    find lines that contain the pattern
  548.     normal f:    find the ':'
  549.     lD        delete the old date and time
  550.     !date^M        read the current date and time into the next line
  551.     kJ        Join the date and time with the previous line
  552.     's        return the cursor to the old position
  553.  
  554. When entering :autocmd on the command line, completion of events and command
  555. names may be done (with <Tab>, CTRL-D, etc.) where appropriate.
  556.  
  557. Vim executes all matching autocommands in the order that you specify them.
  558. It is recommended that your first autocommand be used for all files by using
  559. "*" as the file pattern.  This means that you can define defaults you like
  560. here for any settings, and if there is another matching autocommand it will
  561. override these.  But if there is no other matching autocommand, then at least
  562. your default settings are recovered (if entering this file from another for
  563. which autocommands did match).  Note that "*" will also match files starting
  564. with ".", unlike Unix shells.
  565.  
  566. Autocommands do not change the current search patterns.  Vim saves the current
  567. search patterns before executing autocommands then restores them after the
  568. autocommands finish.  This means that autocommands do not affect the strings
  569. highlighted with the 'hlsearch' option.  Within autocommands, you can still
  570. use search patterns normally, e.g., with the "n" command.
  571.  
  572.  vim:tw=78:ts=8:sw=8:
  573.